<%-- 
    Document   : homework
    Created on : 2019-7-18, 15:59:45
    Author     : dusin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" href="../../js/jquery/themes/default/easyui.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/icon.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/color.css" type="text/css" />
        <link rel="stylesheet" href="../../css/demo.css" type="text/css" />
        <script type="text/javascript" src="../../js/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="../../js/jquery/jquery.easyui.min.js"></script>        
        <script language="JavaScript">
            function formatterStatus(value, row){
                if(value==0){   //这里value的类型是String，不能用‘===’三等号
                    return  '<span style="color:grey;">'+'待提交'+'</span>';                 
                }else if(value==1){
                   return '<span style="color:red;">'+'已截止'+'</span>'; 
                }else if(value==5){
                   return '<span style="color:green;">'+'已发布'+'</span>';
                } 
            }
            $(document).ready(function() {
                $('#tabs').tabs({
                    border: false,
                    onSelect: function(title) {
                        // get the selected tab panel and its tab object
                        var pp = $('#tabs').tabs('getSelected');
                        var selectedTab = pp.panel('options').tab;    // the corresponding tab object
                        var content = selectedTab.content;
                        //alert(content)
                    },
                    onBeforeClose:function(title,index){
                        var rows = $('#dg-detail').datagrid('getChanges');
                        var target = this;
                        if(rows==undefined||rows==null||rows==""){ //没有变化，可以关闭窗口
                            return true;
                        }else{
                            $.messager.confirm('提示','记录已更改，还没有保存！确定要关闭此标签吗？',function(r){ 
                                if (r){
                                    var opts = $(target).tabs('options');
                                    var bc = opts.onBeforeClose;
                                    opts.onBeforeClose = function(){};  // allowed to close now
                                    $(target).tabs('close',index);
                                    opts.onBeforeClose = bc;  // restore the event function
                                }
                            });
                            return false;	// prevent from closing
                        }  
                  }
                });
                $('#dg').datagrid('getPanel').panel('setTitle', courseName);
                $('#dg').datagrid('fitColumns');
            });

        </script>
    </head>
    <body>
        <div id="tabs" class="easyui-tabs" fit="true" border="false">
            <div title="作业管理" style="padding: 5px; overflow: hidden;" id="homework">
                <table id="dg" title="作业管理" class="easyui-datagrid"
                       url="../../servlet/homework/GetHomeworks"
                       toolbar="#toolbar"
                       rownumbers="true" fitColumns="true" singleSelect="true">
                    <thead>
                        <tr>
                            <th field="ID" width="50">ID</th>
                            <th field="no" width="50">序号</th>
                            <th field="title" width="150">名称</th>                            
                            <th field="duedate" width="150">提交截止日期</th>
                            <th field="submitted" width="120">已交</th>
                            <th field="shouldSubmitted" width="120">应交</th>
                            <th field="status" width="100" data-options="formatter:formatterStatus">状态</th>
                            <th field="note" width="100">备注</th>
                            <th field="createTime" width="100">创建日期</th>                        
                        </tr>
                    </thead>
                </table>
                <div id="toolbar">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refreshHomework()">刷新</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newHomework()">增加</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editHomework()">修改</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyHomework()">删除</a>                    
                    <a href="#" class="easyui-linkbutton" iconCls="icon-detail" plain="true" onclick="homeworkDetail()">明细</a> 
                    <a href="#" class="easyui-linkbutton" iconCls="icon-lock" plain="true" onclick="lock()">锁定</a> <!--截止日期到，禁止上传-->
                    <a href="#" class="easyui-linkbutton" iconCls="icon-publish" plain="true" onclick="publish()">发布成绩</a><!--发布成绩-->                    
                    <a href="#" class="easyui-linkbutton" iconCls="icon-packagedownload" plain="true" onclick="downloadAll()">打包下载</a>
                </div>
            </div>
        </div>
        <div id="dlg" class="easyui-dialog" style="width:500px;height:320px;padding:10px 20px"
             closed="true" buttons="#dlg-buttons">
            <div class="ftitle">作业信息</div>
            <form id="fm" method="post">
                <input type ="hidden" name="ID" id="ID"/>
                <input type="hidden" name="teachclassid" id="teachclassid"/>
                <div class="fitem">
                    <label>所属课程:</label>
                    <input id='className' name="className" class="easyui-textbox" readonly="true" >
                </div>
                <div class="fitem">
                    <label>序号:</label>
                    <input id="no" name="no" class="easyui-validatebox" required="true" >
                </div>
                <div class="fitem" class="easyui-validatebox" required="true">
                    <label>作业名称:</label>
                    <input name="title">
                </div>
                <div class="fitem">
                    <label>截止日期:</label>
                    <input id="duedate" name="duedate" type="text" class="easyui-datebox" required="required">
                </div>
                <div class="fitem">
                    <label>备注:</label>
                    <input name="note" >
                </div>
            </form>
        </div>
        <div id="dlg-buttons">
            <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveHomework()">保存</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
        </div>
        <script >
            //alert(parent.teachclassid);
            var courseName = '<%=session.getAttribute("courseName")%>';
            var homeworkId = -1;
            function newHomework() {
                var dd = $('#dg').datagrid('getData')
                $('#dlg').dialog('open').dialog('setTitle', '新作业');
                $('#fm').form('clear');
                $('#className').textbox('setText', courseName);
                $('#teachclassid').val(teachclassid)
                $('#no').val(dd.total + 1);
                var now = new Date();
                now.setDate(now.getDate() + 10);
                $('#duedate').datebox('setValue', now.getMonth() + 1 + '/' + now.getDate() + '/' + now.getFullYear());	// set datebox value
                url = '../../servlet/homework/SaveHomework';
            }

            function saveHomework() {
                $('#fm').form('submit', {
                    url: url,
                    onSubmit: function() {
                        return $(this).form('validate');
                    },
                    success: function(result) {
                        var result = eval('(' + result + ')');
                        if (result.errorMsg) {
                            $.messager.show({
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        } else {
                            $('#dlg').dialog('close');        // close the dialog
                            $('#dg').datagrid('reload');    // reload the user data
                        }
                    }
                });
            }
            function homeworkDetail() {
                //为了简单一次只能打开一个标签。如果已打开明细标签，则不再打开新标签
                var tab = $('#tabs').tabs('getTab' ,1);
                if(tab!=null){
                    $('#tabs').tabs('select' ,1)
                    return;
                }
                var row = $('#dg').datagrid('getSelected');
                if (row) {                    
                    addTab('作业' + (row.no), encodeURI('homeworkdetail.jsp?no=' + (row.no) + '&homeworkId='+row.ID));
                } else {
                    $.messager.show({// show error message
                        title: 'Error',
                        msg: '请先选中一条记录'
                    });
                }


            }
            function editHomework() {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $('#dlg').dialog('open').dialog('setTitle', '编辑');
                    $('#className').textbox('setText', courseName);
                    $('#fm').form('load', row);
                    url = '../../servlet/homework/EditHomework?id=' + row.ID;
                } else {
                    $.messager.show({// show error message
                        title: 'Error',
                        msg: '请先选中一条记录'
                    });
                }
            }
            function destroyHomework() {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('提示', '您确定要删除该次作业吗?', function(r) {
                        if (r) {
                            $.post('../../servlet/homework/DestroyHomework', {id: row.ID}, function(result) {
                                if (result.success) {
                                    $('#dg').datagrid('reload');    // reload the user data
                                } else {
                                    $.messager.show({// show error message
                                        title: 'Error',
                                        msg: result.errorMsg
                                    });
                                }
                            }, 'json');
                        }
                    });
                }
            }
            //发布成绩，学生将可以看到成绩
            function publish(){
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('提示', '您确定要发布该次作业吗？<br/>发布后，学生将看到成绩', function(r) {
                        if (r) {
                            $.post('../../servlet/homework/PublishHomeworkServlet', {id: row.ID}, function(result) {
                                if (result.success) {
                                    $('#dg').datagrid('reload');    // reload the user data
                                } else {
                                    $.messager.show({// show error message
                                        title: 'Error',
                                        msg: result.errorMsg
                                    });
                                }
                            }, 'json');
                        }
                    });
                }
            }
            //锁定作业，禁止提交
            function lock(){
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('提示', '您确定要锁定该次作业吗？<br/>锁定后，学生将无法提交作业', function(r) {
                        if (r) {
                            $.post('../../servlet/homework/LockHomeworkServlet', {id: row.ID}, function(result) {
                                if (result.success) {
                                    $('#dg').datagrid('reload');    // reload the user data
                                } else {
                                    $.messager.show({// show error message
                                        title: 'Error',
                                        msg: result.errorMsg
                                    });
                                }
                            }, 'json');
                        }
                    });
                }
            }
            function refreshHomework() {
                $('#dg').datagrid('reload');    // reload the user data
            }       
            function downloadAll(){
                //alert('downloadAll');
                var row = $('#dg').datagrid('getSelected');                
                //alert(row.path);
                if (row) {                    
                    var no = row.no;
                    var url = "../../servlet/teacher/DownloadAll?type=2&no="+no;
                    open(encodeURI(url));
               
                }else {
                    $.messager.show({// show error message
                        title: 'Error',
                        msg: '请先选中一条记录操作'
                    });
                }
            }
            
            function addTab(subtitle, url) {
                if (!$('#tabs').tabs('exists', subtitle)) {
                    var content = '<iframe name="mainFrame" scrolling="auto" frameborder="0" src="' + url + '" style="width:100%;height:100%;"></iframe>';
                    $('#tabs').tabs('add', {
                        title: subtitle,
                        href: url,
                        closable: true,
                        width: $('#mainPanle').width() - 10,
                        height: $('#mainPanle').height() - 26,
                        cache: true
                    });
                } else {
                    $('#tabs').tabs('select', subtitle);
                }
                tabClose();
            }
            function createFrame(url) {
                var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0" src="' + url + '" style="width:100%;height:100%;"></iframe>';
                return s;
            }
            function tabClose() {
                /*双击关闭TAB选项卡*/
                $(".tabs-inner").dblclick(function() {
                    var subtitle = $(this).children("span").text();
                    $('#tabs').tabs('close', subtitle);
                })
                $(".tabs-inner").bind('contextmenu', function(e) {
                    $('#mm').menu('show', {
                        left: e.pageX,
                        top: e.pageY,
                    });
                    var subtitle = $(this).children("span").text();
                    $('#mm').data("currtab", subtitle);
                    return false;
                });
            }
//绑定右键菜单事件
            function tabCloseEven() {
//关闭当前
                $('#mm-tabclose').click(function() {
                    var currtab_title = $('#mm').data("currtab");
                    $('#tabs').tabs('close', currtab_title);
                })
//全部关闭
                $('#mm-tabcloseall').click(function() {
                    $('.tabs-inner span').each(function(i, n) {
                        var t = $(n).text();
                        $('#tabs').tabs('close', t);
                    });
                });
//关闭除当前之外的TAB
                $('#mm-tabcloseother').click(function() {
                    var currtab_title = $('#mm').data("currtab");
                    $('.tabs-inner span').each(function(i, n) {
                        var t = $(n).text();
                        if (t != currtab_title)
                            $('#tabs').tabs('close', t);
                    });
                });
//关闭当前右侧的TAB
                $('#mm-tabcloseright').click(function() {
                    var nextall = $('.tabs-selected').nextAll();
                    if (nextall.length == 0) {
//msgShow('系统提示','后边没有啦~~','error');
                        alert('后边没有啦~~');
                        return false;
                    }
                    nextall.each(function(i, n) {
                        var t = $('a:eq(0) span', $(n)).text();
                        $('#tabs').tabs('close', t);
                    });
                    return false;
                });
//关闭当前左侧的TAB
                $('#mm-tabcloseleft').click(function() {
                    var prevall = $('.tabs-selected').prevAll();
                    if (prevall.length == 0) {
                        alert('到头了，前边没有啦~~');
                        return false;
                    }
                    prevall.each(function(i, n) {
                        var t = $('a:eq(0) span', $(n)).text();
                        $('#tabs').tabs('close', t);
                    });
                    return false;
                });
//退出
                $("#mm-exit").click(function() {
                    $('#mm').menu('hide');
                })
            }
        </script>
        <style type="text/css">
            #fm{
                margin:0;
                padding:10px 30px;
            }
            .ftitle{
                font-size:14px;
                font-weight:bold;
                padding:5px 0;
                margin-bottom:10px;
                border-bottom:1px solid #ccc;
            }
            .fitem{
                margin-bottom:5px;
            }
            .fitem label{
                display:inline-block;
                width:80px;
            }
            .fitem input{
                width:160px;
            }
        </style>
    </body>
</html>
